package soar.test.pinyin;

import java.util.Arrays;
import java.util.stream.Collectors;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

public class App
{
    public static void main(String[] args)
    {
        new App().run();
    }

    private void run()
    {
        try
        {
            String split = "==================================================";
            System.out.println(split);

            chinese2pinyin();
            System.out.println(split);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void chinese2pinyin() throws Exception
    {
        String chinese = "~!@C罗是个人物，但很SB。^&*";

        HanyuPinyinOutputFormat fmt = new HanyuPinyinOutputFormat();
        fmt.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        fmt.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder strbld = new StringBuilder();
        for (char ch : chinese.toCharArray())
        {
            if (0x4e00 <= ch && ch <= 0x9fa5)
            {
                strbld.append(
                        PinyinHelper.toHanyuPinyinStringArray(ch, fmt)[0]);
            }
            else
            {
                strbld.append(ch);
            }
            System.out.println(strbld.toString());
        }
        System.out
                .println(String.format("%s -> %s", chinese, strbld.toString()));
    }
}
